﻿using System;
using System.Collections.Generic;
using AOPDemo.Aspects;
using AOPDemo.Domain;
using AOPDemo.Messages;
using AOPDemo.Services;

namespace AOPDemo.Program
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            // run this method 3 times.
            for (int i = 0; i < 3; i++ )
                GetAccountsAndTransactions();

            // do a transfer.
            DoTransfer();

            Console.ReadKey();
        }

        private static void GetAccountsAndTransactions()
        {
            var authenticationService = new AuthenticationService();
            var bankingService = new BankingService();

            // authenticate
            AuthToken authToken = authenticationService.Authenticate(new AuthenticateRequest
                                                                         {
                                                                             AuthToken = new AuthToken
                                                                                             {
                                                                                                 UserId =
                                                                                                     "BigBank.JohnSmith"
                                                                                             },
                                                                             UserName = "JohnSmith",
                                                                             Password = "P@assword!"
                                                                         }).AuthToken;

            // get a list of accounts.
            IList<Account> accounts = bankingService.GetAccounts(new GetAccountsRequest
                                                                     {
                                                                         AuthToken = authToken
                                                                     }).Accounts;

            // iterate through the accounts and get account history
            foreach (Account account in accounts)
            {
                Console.WriteLine("Account: " + account.Name);
                IList<Transaction> transactions = bankingService.SearchTransactions(new SearchTransactionsRequest
                                                                                        {
                                                                                            AuthToken = authToken,
                                                                                            AccountId = account.Id,
                                                                                            PagingContext =
                                                                                                new PagingContext
                                                                                                    {
                                                                                                        Page = 1,
                                                                                                        PageSize = 5
                                                                                                    }
                                                                                        }).Transactions;
                // write out the transaction history.
                foreach (Transaction transaction in transactions)
                    Console.WriteLine(transaction.Id);
            }
        }

        private static void DoTransfer()
        {
            var authenticationService = new AuthenticationService();
            var bankingService = new BankingService();

            // authenticate
            AuthToken authToken = authenticationService.Authenticate(new AuthenticateRequest
                                                                         {
                                                                             AuthToken = new AuthToken
                                                                                             {
                                                                                                 UserId =
                                                                                                     "BigBank.JohnSmith"
                                                                                             },
                                                                             UserName = "BigBank",
                                                                             Password = "P@assword!"
                                                                         }).AuthToken;

            // get list of accounts.
            IList<Account> accounts = bankingService.GetAccounts(new GetAccountsRequest
                                                                     {
                                                                         AuthToken = authToken
                                                                     }).Accounts;
            // do a transfer
            bankingService.Transfer(new TransferRequest
                                        {
                                            Amount = 100,
                                            AuthToken = authToken,
                                            FromAccountId = accounts[1].Id,
                                            ToAccountId = accounts[2].Id
                                        });

            Console.WriteLine("Completed transfer!");
        }
    }
}